package com.new_wear.project.category.category.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.category.category.domain.CstCategory;
import com.new_wear.project.category.category.service.ICstCategoryService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;

/**
 * 栏目Controller
 * 
 * @author new_wear
 * @date 2019-12-30
 */
@Controller
@RequestMapping("/category/category")
public class CstCategoryController extends BaseController
{
    private String prefix = "category/category";

    @Autowired
    private ICstCategoryService cstCategoryService;

    @RequiresPermissions("category:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询栏目列表
     */
    @RequiresPermissions("category:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CstCategory cstCategory)
    {
        startPage();
        List<CstCategory> list = cstCategoryService.selectCstCategoryList(cstCategory);
        return getDataTable(list);
    }

    /**
     * 导出栏目列表
     */
    @RequiresPermissions("category:category:export")
    @Log(title = "栏目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstCategory cstCategory)
    {
        List<CstCategory> list = cstCategoryService.selectCstCategoryList(cstCategory);
        ExcelUtil<CstCategory> util = new ExcelUtil<CstCategory>(CstCategory.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 新增栏目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存栏目
     */
    @RequiresPermissions("category:category:add")
    @Log(title = "栏目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CstCategory cstCategory)
    {
        return toAjax(cstCategoryService.insertCstCategory(cstCategory));
    }

    /**
     * 修改栏目
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstCategory cstCategory = cstCategoryService.selectCstCategoryById(id);
        mmap.put("cstCategory", cstCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存栏目
     */
    @RequiresPermissions("category:category:edit")
    @Log(title = "栏目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CstCategory cstCategory)
    {
        return toAjax(cstCategoryService.updateCstCategory(cstCategory));
    }

    /**
     * 删除栏目
     */
    @RequiresPermissions("category:category:remove")
    @Log(title = "栏目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cstCategoryService.deleteCstCategoryByIds(ids));
    }
}
